package labox.innovation.gameserver.model;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import labox.innovation.gameserver.datatables.ItemTable;
import labox.innovation.gameserver.model.actor.instance.FuzePcInstance;
import labox.innovation.gameserver.model.itemcontainer.PcInventory;
import labox.innovation.gameserver.network.SystemMessageId;
import labox.innovation.gameserver.network.clientpackets.RequestAquireskill;
import labox.innovation.gameserver.network.serverpackets.SystemMessage;
import labox.innovation.gameserver.templates.StatsSet;
import labox.innovation.gameserver.templates.item.L2Item;
import labox.innovation.gameserver.templates.item.FuzeEconomicManager.OPSTATE;

/**
 * This class ...
 * 技能学习条件
 * version1.0 2010 1.6
 *
 * @version $Revision: 1.2.4.2 $ $Date: 2005/03/27 15:29:33 $
 */
public class L2SkillLearn {
	
	private static final Logger _log = LoggerFactory.getLogger(L2SkillLearn.class.getName());
	// these two build the primary key
	//技能id
	protected final int _id;

	//技能等级
	protected final int _level;

	// not needed, just for easier debug
	// 技能名字
	protected final String _name;

	//最低角色等级
	protected final int _plv;

	//是否结婚
	protected final boolean _ismarry;

	//前置技能 id 和 等级
	protected final int _pre_skill_id[];

	protected final int _pre_skill_lv[];

	//需要的物品 id 和 数目
	protected final int _need_item_id[];

	protected final int _need_item_num[];

	//宠物等级
	protected final int _pet_lv;

	//帮会等级
	protected final int _clan_lv;

	//称号id需求
	protected final int _label_id;

	//经验消耗
	protected final int _exp_consume;

	protected int _classid; //职业id (每个职业的仙魔位都不一样)

	protected int _moneySilverNums; //需要银文的数量

	protected int _moneyNoteNums; //需要银票的数量

	protected int _moneyYuanBaoNums; //需要元宝的个数

	//消耗的功勋值
	//protected final int _geste_consume;
	//消耗的荣誉值
	//protected final int _honor_consume;
	//家族等级
	//protected final int _gangs_lv;

	public L2SkillLearn(StatsSet set) {
		_id = set.getInteger("id");
		_level = set.getInteger("lvl");
		_plv = set.getInteger("plv", 0);
		_name = set.getString("name", "").intern();
		_ismarry = set.getBool("ismarry", false);
		_pre_skill_id = set.getIntegerArray("pre_skill_id", null);
		_pre_skill_lv = set.getIntegerArray("pre_skill_lv", null);
		_need_item_id = set.getIntegerArray("need_item_id", null);
		_need_item_num = set.getIntegerArray("need_item_num", null);
		_pet_lv = set.getInteger("pet_lv", 0);
		_clan_lv = set.getInteger("clan_lv", 0);
		_label_id = set.getInteger("label_id", -1);
		_exp_consume = set.getInteger("exp_consume", 0);

		_classid = set.getInteger("class_need", -1);

		if (_need_item_id != null && _need_item_num != null) {
			for (int i = 0; i < _need_item_id.length; i++) {
				if (_need_item_id[i] == PcInventory.MONEY_SLIVER_ID) {
					_moneySilverNums = _need_item_num[i];
				} else if (_need_item_id[i] == PcInventory.MONEY_NOTE_ID) {
					_moneyNoteNums = _need_item_num[i];
				} else if (_need_item_id[i] == PcInventory.MONEY_YAUNBAO_ID) {
					_moneyYuanBaoNums = _need_item_num[i];
				}
			}
		}

	}

	/**
	 * 技能学习条件
	 *
	 */
	public L2SkillLearn(int id, int lvl, int minLvl, String name, boolean isMarry, int pre_skill_id[], int pre_skill_lv[], int need_item_id[], int need_item_num[], int pet_lv, int clan_lv, int label_id, int exp_consume) {
		_id = id;
		_level = lvl;
		_plv = minLvl;
		_name = name.intern();
		_ismarry = isMarry;
		_pre_skill_id = pre_skill_id;
		_pre_skill_lv = pre_skill_lv;
		_need_item_id = need_item_id;
		_need_item_num = need_item_num;
		_pet_lv = pet_lv;
		_clan_lv = clan_lv;
		_label_id = label_id;
		_exp_consume = exp_consume;
	}

	/**
	 *  玩家 是否能学这个技能
	 * @return
	 */
	public boolean canLearn(FuzePcInstance player) {
		if (player.getLevel() < _plv) {
			player.sendPacket( new SystemMessage( SystemMessageId.NOT_ENOUGH_LEVEL  ) );
			return false;
		}

		if (player.getClan() != null && player.getClan().getLevel() < _clan_lv) {
			return false;
		}

		if (player.getExp() < _exp_consume) {
			player.sendPacket( new SystemMessage( SystemMessageId.NOT_ENOUGH_EXP  ) );
			return false;
		}

		if (_ismarry && !player.isMarried()) {
			player.sendPacket( new SystemMessage( SystemMessageId.NOT_MARRAY  ) );
			return false;
		}

		//检查前置技能限制
		if (_pre_skill_id != null && _pre_skill_lv != null && _pre_skill_id.length == _pre_skill_lv.length) {
			for (int i = 0; i < _pre_skill_id.length; i++) {
				int lvl = player.getSkillLevel(_pre_skill_id[i]);
				if (lvl != _pre_skill_lv[i]) {
					player.sendPacket( new SystemMessage( SystemMessageId.NOT_PRE_SKILL  ) );
					return false;
				}
			}
		}

		L2Item it= null;
		//检查物品道具限制
		if (_need_item_id != null && _need_item_num != null && _need_item_id.length == _need_item_num.length) {
			for (int i = 0; i < _need_item_id.length; i++) {
//				List<L2ItemInstance> items = player.getInventory().getItemsByItemId(_need_item_id[i]);
				
				int nums = (int) player.getInventory().getInventoryItemCount(_need_item_id[i]);
				it = ItemTable.getInstance().getTemplate( _need_item_id[i] );
				
				if( _log.isDebugEnabled() ){
					_log.debug("技能:" + _name + "id:" + _id + "学习需要道具:" + _need_item_id[i] + "数量:" + _need_item_num[i] + "现有数量:" + nums );
				}
				
				if( it.isMoney() ){//如果是钱
					
					if( !player.hasEnoughSilverMoney( OPSTATE.LEARNSKILL.getValue(), _need_item_num[i] ) ){
						player.sendPacket( new SystemMessage(SystemMessageId.YOU_NOT_ENOUGH_ADENA) );
						return false;
					}
					
				}else{ //不是钱
					
					if( nums < _need_item_num[i] ){
						
						SystemMessage sm = new SystemMessage( SystemMessageId.S1_S2_NOT_ENOUGH_ITEMS );
						sm.addItemName(  _need_item_id[i] );
						sm.addNumber(1);
						player.sendPacket( sm );
						
						return false;
						
					}// end 
					
				}
				
				
//				if (nums < _need_item_num[i]) {
//					if( it != null && it.isMoney() ){
//						player.sendPacket( new SystemMessage(SystemMessageId.YOU_NOT_ENOUGH_ADENA) );
//					}
//					else{
//						SystemMessage sm = new SystemMessage( SystemMessageId.S1_S2_NOT_ENOUGH_ITEMS );
//						sm.addItemName(  _need_item_id[i] );
//						sm.addNumber(1);
//						player.sendPacket( sm );
//					}
//
//					return false;
//				}
				
			}// end for
			
		}

		return true;
	}

	/**
	 * @return Returns the id.
	 */
	public int getId() {
		return _id;
	}

	/**
	 * @return Returns the level.
	 */
	/***
	 * 返回这个技能的等级
	 */
	public int getLevel() {
		return _level;
	}

	/**
	 * @return Returns the minLevel.
	 */
	/***
	 * 返回技能的最低学习等级
	 */
	public int getMinLevel() {
		return _plv;
	}

	/**
	 * @return Returns the name.
	 */
	public String getName() {
		return _name;
	}

	/**
	 * 返回技能所需要的所有物品
	 * @return
	 */
	public int[] getItems() {
		return _need_item_id;
	}

	public int[] getItemNums() {
		return _need_item_num;
	}

	/**
	 * 获得 需要的银文数
	 * @return
	 */
	public int getMoneySilverNums() {
		return _moneySilverNums;
	}

	/**
	 * 获得需要的银票数
	 * @return
	 */
	public int getMoneyNoteNums() {
		return _moneyNoteNums;
	}

	/**
	 * 获得需要的元宝数
	 * @return
	 */
	public int getMoneyYuanBaoNums() {
		return _moneyYuanBaoNums;
	}

	/**
	 * 返回所需要的前置技能
	 * @return
	 */
	public int[] getPreSkills() {
		return _pre_skill_id;
	}

	public int[] getpre_skill_lvs() {
		return _pre_skill_lv;
	}

	/**
	 * 返回是否结婚
	 * @return
	 */
	public boolean getIsMarry() {
		return _ismarry;
	}

	/**
	 * 返回宠物等级
	 * @return
	 */
	public int getpet_lv() {
		return _pet_lv;
	}

	/**
	 * 返回帮会等级
	 * @return
	 */
	public int getclan_lv() {
		return _clan_lv;
	}

	/**
	 * 返回需求的称号id
	 * @return
	 */
	public int getLabel_id() {
		return _label_id;
	}

	/**
	 *  需要消耗的经验值
	 * @return
	 */
	public int getExp_consume() {
		return _exp_consume;
	}

	/**
	 *  获得这个仙魔位技能的 职业id
	 * @return
	 */
	public int getClassId() {
		return _classid;
	}

}
